<?php
/*------------------------------------------
* @AUTHOR: TANDAT @EMAIL:tidusvn05@gmail.com
* @PHONE: +84933731173
* -----------------------------------------*/
class D_Helper {
	
	
	static function inputText($name,$class,$defaul_value,$more=''){
		$result = "<input name=\"{$name}\" id=\"{$name}\" type=\"text\" class=\"{$class}\" ";
		if ($defaul_value){ $result.=" value=\"{$defaul_value}\" ";}
		if ($more!=''){ $result.=" {$more} ";}
		$result .=" />";
		return $result;
	}

	static function inputText2($name,$id,$class,$defaul_value,$more=''){
		$result = "<input name=\"{$name}\" id=\"{$id}\" type=\"text\" class=\"{$class}\" ";
		if ($defaul_value){ $result.=" value=\"{$defaul_value}\" ";}
		if ($more!=''){ $result.=" {$more} ";}
		$result .=" />";
		return $result;
	}
	
	static function inputFile($name,$class,$defaul_value='',$more=''){
		$result = "<input name=\"{$name}\" id=\"{$name}\" type=\"file\" class=\"{$class}\" ";
		if ($defaul_value){ $result.=" value=\"{$defaul_value}\" ";}
		if ($more!=''){ $result.=" {$more} ";}
		$result .=" />";
		return $result;
	}
	
	static function inputPassword($name,$class,$defaul_value,$more=''){
		$result = "<input name=\"{$name}\" id=\"{$name}\" type=\"password\" class=\"{$class}\" ";
		if ($defaul_value){ $result.=" value=\"{$defaul_value}\" ";}
		if ($more!=''){ $result.=" {$more} ";}
		$result .=" />";
		return $result;
	}
	
	static function textArea($name,$class,$defaul_value,$title="",$style=""){
		$result = "<textarea name=\"{$name}\" id=\"{$name}\" class=\"{$class}\" >";
		if (isset($defaul_value)){ $result.="{$defaul_value}";}
		$result .="</textarea>";
		return $result;
	}
	
	static function inputCheckbox($name,$class,$defaul_value,$id="",$title="",$style=""){
		$result = "<input name=\"{$name}\" type=\"checkbox\" class=\"{$class}\" ";
		if($id!=''){$result.=" id=\"{$id}\" ";}
		if ($defaul_value&&$defaul_value!=''){ $result.=" checked=\"\" ";}
		
		$result .=" />";
		return $result;
	}
	
	static function inputRadio($name,$class,$value='',$select_value='',$id="",$more=''){
		$result = "<input name=\"{$name}\" value=\"{$value}\" type=\"radio\" class=\"{$class}\" ";
		if($id!=''){$result.=" id=\"{$id}\" ";}
		if($value!=''&&$value==$select_value){$result.=" checked=\"checked\" ";}
		if ($more!=''){ $result.=" {$more} ";}
		$result .=" />";
		return $result;
	}
	static function select($name,$class,$default_value,$id,$more=''){
		$result = "<select name=\"$name\" class=\"$class\" ";
	}
	
	static function selectState($name,$class,$default_value,$more=''){
		$adapter = Zend_Db_Table::getDefaultAdapter();
		$areas = $adapter->fetchAll("select * from area where DeleteFlag = 0");
		$result = "<select name=\"$name\" class=\"$class\" ";
		if($more!=''){$result .= " {$more} ";}
		$result .= " >";
		
		$result .= "<option value=''>都道府県</option>";
		foreach ($areas as $area){
			$name = $area['Name'];
			$result .= "<optgroup label=\"{$name}\">";
			$states = $adapter->fetchAll("select * from state where AreaID = {$area['AreaID']} and DeleteFlag = 0");
			foreach ($states as $state){
				if($default_value==$state['StateID'])
					$result .= "<option value='{$state['StateID']}' selected=\"selected\">{$state['Name']}</option>";
				else 
					$result .= "<option value='{$state['StateID']}'>{$state['Name']}</option>";
			}
			$result .= "</optgroup>";
		}
		
		$result.="</select>";
		
		return $result;
	}
	
	static function selectState2($name,$class,$default_value,$more=''){
		$adapter = Zend_Db_Table::getDefaultAdapter();
		$areas = $adapter->fetchAll("select * from area where DeleteFlag = 0");
		$result = "<select name=\"$name\" class=\"$class\" ";
		if($more!=''){$result .= " {$more} ";}
		$result .= " >";
		$result .= "<option value='-1' disabled=''>都道府県</option>";
		foreach ($areas as $area){
			$name = $area['Name'];
			$result .= "<optgroup label=\"{$name}\">";
			$states = $adapter->fetchAll("select * from state where AreaID = {$area['AreaID']} and DeleteFlag = 0");
			foreach ($states as $state){
				if($default_value==$state['StateID'])
					$result .= "<option value='{$state['StateID']}' selected=\"selected\">{$state['Name']}</option>";
				else 
					$result .= "<option value='{$state['StateID']}'>{$state['Name']}</option>";
			}
			$result .= "</optgroup>";
		}
		
		$result.="</select>";
		
		return $result;
	}
	
	static function selectBusinessArea($name,$class,$default_value,$more=''){
		$adapter = Zend_Db_Table::getDefaultAdapter();
		$result = "<select name=\"$name\" class=\"$class\" ";
		if($more!=''){$result .= " {$more} ";}
		$result .= " >";
		
		$result .= "<option value=''>分類</option>";
		$business_areas = $adapter->fetchAll("select * from businessarea where DeleteFlag = 0");
		
		foreach ($business_areas as $ba){
			if($default_value==$ba['BusinessAreaID'])
				$result .= "<option value='{$ba['BusinessAreaID']}' selected=\"selected\">{$ba['BusinessAreaValue']}</option>";
			else 
				$result .= "<option value='{$ba['BusinessAreaID']}'>{$ba['BusinessAreaValue']}</option>";
		}
		//$result .="<option value='other'>その他</option>";//orther
		$result.="</select>";
		
		return $result;
	}
	
	static function selectBudget($name,$class,$default_value,$more=''){
		$adapter = Zend_Db_Table::getDefaultAdapter();
		$result = "<select name=\"$name\" class=\"$class\" ";
		if($more!=''){$result .= " {$more} ";}
		$result .= " >";
		
		$result .= "<option value=''>ご予算を選択してください</option>";
		$business_areas = $adapter->fetchAll("select * from budget where DeleteFlag = 0");
		
		foreach ($business_areas as $ba){
			if($default_value==$ba['BudgetID'])
				$result .= "<option value='{$ba['BudgetID']}' selected=\"selected\">{$ba['BudgetValue']}</option>";
			else 
				$result .= "<option value='{$ba['BudgetID']}'>{$ba['BudgetValue']}</option>";
		}
		//$result .="<option value='other'>その他</option>";//orther
		$result.="</select>";
		return $result;
	}
	
	static function selectBusinessArea2($name,$class,$default_value,$more=''){
		$adapter = Zend_Db_Table::getDefaultAdapter();
		$result = "<select name=\"$name\" class=\"$class\" ";
		if($more!=''){$result .= " {$more} ";}
		$result .= " >";
		if($default_value < 1)
			$result .= "<option disabled='' selected='selected' value='-1'>分類</option>";
		else $result .= "<option disabled='' value=''>分類</option>";
		$business_areas = $adapter->fetchAll("select * from businessarea where DeleteFlag = 0");
		
		foreach ($business_areas as $ba){
			if($default_value==$ba['BusinessAreaID'])
				$result .= "<option value='{$ba['BusinessAreaID']}' selected=\"selected\">{$ba['BusinessAreaValue']}</option>";
			else 
				$result .= "<option value='{$ba['BusinessAreaID']}'>{$ba['BusinessAreaValue']}</option>";
		}
		//$result .="<option value='other'>その他</option>";//orther
		$result.="</select>";
		
		return $result;
	}
	
	
	static function selectDivision($name,$class,$default_value,$more=''){
		$adapter = Zend_Db_Table::getDefaultAdapter();
		$result = "<select name=\"$name\" class=\"$class\" ";
		if($more!=''){$result .= " {$more} ";}
		$result .= " >";
		
		$result .= "<option value=''>分類</option>";
		
		$business_areas = $adapter->fetchAll("select * from division where DeleteFlag = 0");
		
		foreach ($business_areas as $di){
			if($default_value==$di['DivisionID'])
				$result .= "<option value='{$di['DivisionID']}' selected=\"selected\">{$di['DivisionValue']}</option>";
			else 
				$result .= "<option value='{$di['DivisionID']}'>{$di['DivisionValue']}</option>";
		}
		
		$result.="</select>";
		
		return $result;
	}
	
	static function selectDivision2($name,$class,$default_value,$more=''){
		$adapter = Zend_Db_Table::getDefaultAdapter();
		$result = "<select name=\"$name\" class=\"$class\" ";
		if($more!=''){$result .= " {$more} ";}
		$result .= " >";
		if($default_value < 1)
			$result .= "<option disabled='' selected='selected' value='-1'>分類</option>";
		else $result .= "<option disabled='' value=''>分類</option>";
		$business_areas = $adapter->fetchAll("select * from division where DeleteFlag = 0");
		
		foreach ($business_areas as $di){
			if($default_value==$di['DivisionID'])
				$result .= "<option value='{$di['DivisionID']}' selected=\"selected\">{$di['DivisionValue']}</option>";
			else 
				$result .= "<option value='{$di['DivisionID']}'>{$di['DivisionValue']}</option>";
		}
		
		$result.="</select>";
		
		return $result;
	}
	
	static function selectPosition($name,$class,$default_value,$more=''){
		$adapter = Zend_Db_Table::getDefaultAdapter();
		$result = "<select name=\"$name\" class=\"$class\" ";
		if($more!=''){$result .= " {$more} ";}
		$result .= " >";
		$result .= "<option value=''>分類</option>";
		$business_areas = $adapter->fetchAll("select * from position where DeleteFlag = 0");
		
		foreach ($business_areas as $po){
			if($default_value==$po['PositionID'])
				$result .= "<option value='{$po['PositionID']}' selected=\"selected\">{$po['PositionValue']}</option>";
			else 
				$result .= "<option value='{$po['PositionID']}'>{$po['PositionValue']}</option>";
		}
		
		$result.="</select>";
		
		return $result;
	}
	
	static function selectPosition2($name,$class,$default_value,$more=''){
		$adapter = Zend_Db_Table::getDefaultAdapter();
		$result = "<select name=\"$name\" class=\"$class\" ";
		if($more!=''){$result .= " {$more} ";}
		$result .= " >";
		if($default_value < 1)
			$result .= "<option disabled='' selected='selected' value='-1'>分類</option>";
		else $result .= "<option disabled='' value=''>分類</option>";
		$business_areas = $adapter->fetchAll("select * from position where DeleteFlag = 0");
		
		foreach ($business_areas as $po){
			if($default_value==$po['PositionID'])
				$result .= "<option value='{$po['PositionID']}' selected=\"selected\">{$po['PositionValue']}</option>";
			else 
				$result .= "<option value='{$po['PositionID']}'>{$po['PositionValue']}</option>";
		}
		
		$result.="</select>";
		
		return $result;
	}
	
	static function selectScaleEmployees($name,$class,$default_value,$more=''){
		$adapter = Zend_Db_Table::getDefaultAdapter();
		$result = "<select name=\"$name\" class=\"$class\" ";
		if($more!=''){$result .= " {$more} ";}
		$result .= " >";
		$result .= "<option value=''>対象企業規模</option>";
		$arr = $adapter->fetchAll("select * from scaleemployee where DeleteFlag = 0");
		
		foreach ($arr as $po){
			if($default_value==$po['ScaleEmployeeID'])
				$result .= "<option value='{$po['ScaleEmployeeID']}' selected=\"selected\">{$po['ScaleEmployeeValue']}</option>";
			else 
				$result .= "<option value='{$po['ScaleEmployeeID']}'>{$po['ScaleEmployeeValue']}</option>";
		}
		
		$result.="</select>";
		
		return $result;
	}
	
	static function selectCategory($name,$class,$default_value,$more=''){
		$list =  D_Utils::getAllCategory();
		//$list = new ArrayObject($list, ArrayObject::ARRAY_AS_PROPS);
		$result = "<select name=\"$name\" class=\"$class\" ";
		if($more!=''){$result .= " {$more} ";}
		$result .= " >";
		$result .= "<option value=''>カテゴリを選択してください</option>";
		foreach ($list as $obj){
			$result.='<optgroup label="'.$obj['Name'].'">';
			$list_sub = $obj['List'];
			foreach ($list_sub as $obj2){
				$result .= "<optgroup label='&nbsp;&nbsp;{$obj2['Name']}'></option>";
				$list_small = $obj2['List'];
				foreach ($list_small as $obj3){
					if($default_value== $obj3['CategoryID'])
						$result .= "<option value='{$obj3['CategoryID']}' selected=\"selected\">{$obj3['Name']}</option>";
					else 
						$result .= "<option value='{$obj3['CategoryID']}'>{$obj3['Name']}</option>";
				}
			}		
					
			$result.='</optgroup>';
		}
		
		$result.="</select>";
		return $result;
	}
	
	static function getStateNameFromID($id){
		$adapter = Zend_Db_Table::getDefaultAdapter();
		$row = $adapter->fetchRow("select * from state where {$adapter->quoteInto("StateID = ?", $id)} and  DeleteFlag = 0");
		return $row['Name'];
	}
	
	static function getAreaIDFromStateID($id){
		$adapter = Zend_Db_Table::getDefaultAdapter();
		$row = $adapter->fetchRow("select * from state where {$adapter->quoteInto("StateID = ?", $id)} and  DeleteFlag = 0");
		return $row['AreaID'];
	}
	
	static function getBusinessValueFromID($id){
		$adapter = Zend_Db_Table::getDefaultAdapter();
		$row = $adapter->fetchRow("select * from businessarea where {$adapter->quoteInto("BusinessAreaID = ?", $id)} and  DeleteFlag = 0");
		return $row['BusinessAreaValue'];
	}
	
	static function getDivisionValueFromID($id){
		$adapter = Zend_Db_Table::getDefaultAdapter();
		$row = $adapter->fetchRow("select * from division where {$adapter->quoteInto("DivisionID = ?", $id)} and  DeleteFlag = 0");
		return $row['DivisionValue'];
	}
	
	static function getPositionValueFromID($id){
		$adapter = Zend_Db_Table::getDefaultAdapter();
		$row = $adapter->fetchRow("select * from position where {$adapter->quoteInto("PositionID = ?", $id)} and  DeleteFlag = 0");
		return $row['PositionValue'];
	}
	
	static function getCategoryValueFromID($id){
		$adapter = Zend_Db_Table::getDefaultAdapter();
		$row = $adapter->fetchRow("select * from category where {$adapter->quoteInto("CategoryID = ?", $id)} and  DeleteFlag = 0");
		return $row['Name'];
	}
	
	static function getBudgetValueFromID($id){
		$adapter = Zend_Db_Table::getDefaultAdapter();
		$row = $adapter->fetchRow("select * from budget where {$adapter->quoteInto("BudgetID = ?", $id)} and  DeleteFlag = 0");
		return $row['BudgetValue'];
	}
	
	static function getScaleEmployeeValueFromID($id){
		$adapter = Zend_Db_Table::getDefaultAdapter();
		$row = $adapter->fetchRow("select * from scaleemployee where {$adapter->quoteInto("ScaleEmployeeID = ?", $id)} and  DeleteFlag = 0");
		return $row['ScaleEmployeeValue'];
	}
	
	// admin category helper
	static function selectAdminCategory($name,$class,$default_value,$more=''){
		$adapter = Zend_Db_Table::getDefaultAdapter();
		$result = "<select name=\"$name\" class=\"$class\" ";
		if($more!=''){$result .= " {$more} ";}
		$result .= " >";
		$result .= "<option value=''>カテゴリ</option>";
		
		$category = new Category();
		$filter = array("type"=>"level1");
		$list = $category->listByFilter($filter);
		
		foreach ($list as $obj){
			if($default_value==$obj->CategoryID)
				$result .= "<option value='{$obj->CategoryID}' selected=\"selected\">{$obj->Name}</option>";
			else 
				$result .= "<option value='{$obj->CategoryID}'>{$obj->Name}</option>";
		}
		
		$result.="</select>";
		
		return $result;
	}
	static function selectAdminSubCategory($name,$class,$default_value,$more=''){
		$adapter = Zend_Db_Table::getDefaultAdapter();
		$result = "<select name=\"$name\" class=\"$class\" ";
		if($more!=''){$result .= " {$more} ";}
		$result .= " >";
		$result .= "<option value=''>カテゴリ</option>";
		
		$category = new Category();
		$filter = array("type"=>"level1");
		$list = $category->listByFilter($filter);
		
		foreach ($list as $obj){
			$result.='<optgroup label="'.$obj->Name.'">';
			$filter = array("type"=>"level2","parent_id" => $obj->CategoryID );
			$list_sub = $category->listByFilter($filter);
			foreach ($list_sub as $obj2){
				if($default_value==$obj2->CategoryID)
					$result .= "<option value='{$obj2->CategoryID}' selected=\"selected\">{$obj2->Name}</option>";
				else 
					$result .= "<option value='{$obj2->CategoryID}'>{$obj2->Name}</option>";
			}		
					
			$result.='</optgroup>';
		}
		
		$result.="</select>";
		
		return $result;
	}
	
	static function selectServicePublishStatus($name,$class,$default_value='',$more=''){
		$ini = new Zend_Config_Ini('./application/configs/application.ini', 'servicepublishstatus');
		$arr = $ini->toArray();
		
		$result = "<select name=\"$name\" class=\"$class\" ";
		if($more!=''){$result .= " {$more} ";}
		$result .= " >";
						
		foreach ($arr as $key=>$val){
			if($default_value==$key)
				$result .= "<option value='{$key}' selected=\"selected\">{$val}</option>";
			else 
				$result .= "<option value='{$key}'>{$val}</option>";
		}
		
		$result.="</select>";
		
		return $result;
	}
	
	static function selectServiceEditStatus($name,$class,$default_value='',$more=''){
		$ini = new Zend_Config_Ini('./application/configs/application.ini', 'serviceeditstatus');
		$arr = $ini->toArray();
		
		$result = "<select name=\"$name\" class=\"$class\" ";
		if($more!=''){$result .= " {$more} ";}
		$result .= " >";
						
		foreach ($arr as $key=>$val){
			if($default_value==$key)
				$result .= "<option value='{$key}' selected=\"selected\">{$val}</option>";
			else 
				$result .= "<option value='{$key}'>{$val}</option>";
		}
		
		$result.="</select>";
		
		return $result;
	}
	
	static function selectServicerStatus($name,$class,$default_value='',$more='', $except=array()){
		$ini = new Zend_Config_Ini('./application/configs/application.ini', 'servicerstatus');
		$arr = $ini->toArray();
		
		$result = "<select name=\"$name\" class=\"$class\" ";
		if($more!=''){$result .= " {$more} ";}
		$result .= " >";
						
		foreach ($arr as $key=>$val)
		{
			if (in_array($key, $except))
				continue;
				
			if($default_value==$key)
				$result .= "<option value='{$key}' selected=\"selected\">{$val}</option>";
			else 
				$result .= "<option value='{$key}'>{$val}</option>";
		}
		
		$result.="</select>";
		
		return $result;
	}
	
	static function selectServicerStatus_Include($name,$class,$default_value='',$more='', $include=array()){
		$ini = new Zend_Config_Ini('./application/configs/application.ini', 'servicerstatus');
		$arr = $ini->toArray();
		
		$result = "<select name=\"$name\" class=\"$class\" ";
		if($more!=''){$result .= " {$more} ";}
		$result .= " >";
						
		foreach ($arr as $key=>$val)
		{
			if (!in_array($key, $include))
				continue;
				
			if($default_value==$key)
				$result .= "<option value='{$key}' selected=\"selected\">{$val}</option>";
			else 
				$result .= "<option value='{$key}'>{$val}</option>";
		}
		
		$result.="</select>";
		
		return $result;
	}
	
	static function getServicePublishStatusValueFromID($id){
		$ini = new Zend_Config_Ini('./application/configs/application.ini', 'servicepublishstatus');
		$arr = $ini->toArray();
		return isset($arr[$id])?$arr[$id]:'';
	}
	
	static function getServiceEditStatusValueFromID($id){
		$ini = new Zend_Config_Ini('./application/configs/application.ini', 'serviceeditstatus');
		$arr = $ini->toArray();
		return isset($arr[$id])?$arr[$id]:'';
	}
	
	static function getServicerStatusValueFromID($id){
		$ini = new Zend_Config_Ini('./application/configs/application.ini', 'servicerstatus');
		$arr = $ini->toArray();
		return isset($arr[$id])?$arr[$id]:'';
	}
	
	static function upload($upload, $field){			
		try 
		{
        	$upload->receive($field);        	
	    } 
	    catch (Zend_File_Transfer_Exception $e) 
	    { 
	        return array($e->getMessage()); 
	    }
	}
	
	static function createUpload($extension, $maxBytes, $maxWidth, $maxHeight, $path, $field){			
		$upload = new Zend_File_Transfer_Adapter_Http();
		
		if (!is_dir($path))
			mkdir($path);
		
		$upload->addValidator("Count", false, 1)
		->addValidator("Extension", false, $extension)
		->addValidator("Size", false, $maxBytes)
		->addValidator("ImageSize", false, array("maxwidth"=>$maxWidth, "maxheight"=>$maxHeight))
		->setDestination($path);

		$upload->addFilter("Rename", array("target"=>$path . '/' . $upload->getFileName($field, false), "overwrite"=>true));
		
		return $upload;
	}	
}

?>